package com.weizhu.swufer.core.metrics;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class RpcMetrics {

    private String identityName;

    private final static Logger logger = LoggerFactory.getLogger(RpcMetrics.class);

    private final AtomicInteger active = new AtomicInteger();
    private final AtomicLong total = new AtomicLong();
    private final AtomicInteger failed = new AtomicInteger();

    public RpcMetrics(String identityName){
        this.identityName = identityName;
    }

    public void beginStat(){
        active.incrementAndGet();
    }

    public void endStat(boolean success){
        active.decrementAndGet();
        total.incrementAndGet();
        if (!success){
            failed.incrementAndGet();
        }
    }

    public AtomicInteger getActive() {
        return active;
    }

    public boolean failOverThreshold(){
        return failed.get() > 10 && (float) failed.get() / total.get() > 0.2;
    }

    public void clear(){
        active.set(0);
        failed.set(0);
        total.set(0);
    }
}
